package net.bxx2004.extand.util;

import net.bxx2004.extand.manager.ConfigManager;

import java.sql.*;

public class JDBCUtil {
    private static Connection connection;
    private static String url = ConfigManager.getSetting().getString("Storge.SqlIP");
    private  static String username = ConfigManager.getSetting().getString("Storge.SqlUsername");
    private  static String password = ConfigManager.getSetting().getString("Storge.SqlPassword");
    private static String database = ConfigManager.getSetting().getString("Storge.SqlDatabase");
    //链接
    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String[] urls = url.split(":");
            String ip = "jdbc:mysql://"+ urls[0] + ":" + urls[1] +"/"+  database;
            return connection = DriverManager.getConnection(ip, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭
    public static void close(ResultSet rs, Statement stmt, Connection connection, PreparedStatement statement){
        try{
            if(rs!=null) {
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        try{
            if(stmt!=null) {
                stmt.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        try{
            if(connection != null) {
                connection.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        try{
            if(statement != null) {
                statement.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

    }
}
